#include  <cstdio>
#include  <map>
#include  <vector>
#include  <iostream>
#include  <algorithm>
#include  <string>
#include  <cstring>
using namespace std;

map<string,int> dic;

void ff(char *ss, int k, vector<string> &arr)
{
	if(k==0)
	{
		arr.push_back(ss);
		return;
	}

	for( int i = 0; i <= k ; ++i )
	{
		char temp = ss[i];
		ss[i] = ss[k];
		ss[k] = temp;

		ff(ss,k-1,arr);

		temp = ss[i];
		ss[i] = ss[k];
		ss[k] = temp;
	}
	
}

void fn(char *ss, int len, vector<string> &arr)
{
	ff(ss,len-1,arr);
}

int main(int argc, char *argv[])
{
	char ss[7];
	scanf("%s",ss);
	while(strcmp(ss,"XXXXXX") != 0)
	{
		dic[ss] = 1;
		scanf("%s",ss);
	}

	int res;
	scanf("%s",ss);
	while(strcmp(ss,"XXXXXX") != 0)
	{
		vector<string> arr;
		int len = strlen(ss);
		
		fn(ss,len,arr);
			
		sort(arr.begin(),arr.end());
		string pre = arr[0];
		int n = arr.size();
		bool yes = false;

		for( int i = 0 ; i < n-1  ; ++i )
			if( arr[i] != arr[i+1] )
				if(dic.find(arr[i]) != dic.end())
				{	
					cout << arr[i] << endl;
					yes = true;
				}
		if(dic.find(arr[n-1]) != dic.end() )
		{
			cout << arr[n-1] << endl;
			yes = true;
		}
		
		if(!yes)
			printf("NOT A VALID WORD\n");

		printf("******\n");
		scanf("%s",ss);
	}
}
